package cn.tedu.socket;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * 多个人可以说多个话
 */
public class ServerThreadMorePeopleMoreLine {
    public static void main(String[] args) throws Exception {
        ServerSocket serversocket = new ServerSocket(8888);
        System.out.println("等待连接");
        Socket socket = null;
        while (true){
            socket = serversocket.accept();
            System.out.println("连接成功");
            jieDai(socket);
        }
    }
    // 多线程去处理
    public static void jieDai(Socket socket) throws Exception {
        Thread thread = new Thread(){
            @Override
            public void run() {
                try{
                    // 读取内容并输出
                    InputStream in = socket.getInputStream();
                    InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
                    BufferedReader br = new BufferedReader(isr);
                    String line = null;
                    while((line= br.readLine())!=null){
                        System.out.println(line);
                    }
                    br.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        thread.start();
    }
}
